﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;

namespace CY_MVC.ViewTemplate.Default
{
    //模板页面
    //<%@ Control Language="C#" AutoEventWireup="false" Inherits="CY_MVC.ViewTemplate.Default.TemplateBody" %>
    class ViewTemplate : BaseViewTemplate
    {
        public ViewTemplate()
        {
            Extension = ".ascx";
        }

        private TemplateBody m_Template;

        private TemplateBody Template
        {
            get
            {
                return m_Template;
            }
            set
            {
                m_Template = value;
            }
        }

        public override void SetTemplateFile(string p_TemplateFile)
        {
            base.SetTemplateFile(p_TemplateFile);
            var template = (new UserControl().LoadControl(TemplateFile.Replace("\\", "/")) as TemplateBody);
            if (template != null)
            {
                Template = template;
            }
            else
            {
                throw new Exception(TemplateFile + @"页面未设置<%@ Control Language=""C#"" AutoEventWireup=""false"" Inherits=""CY_MVC.ViewTemplate.Default.TemplateBody"" %>");
            }
        }

        public virtual void SetTemplate(object p_ViewTemplate)
        {
            var targetViewTemplate = p_ViewTemplate as ViewTemplate;
            if (targetViewTemplate != null) base.SetTemplateFile(targetViewTemplate.TemplateFile);
            var template = new UserControl().LoadControl(TemplateFile.Replace("\\", "/")) as TemplateBody;
            if (template != null)
            {
                Template = template;
            }
            else
            {
                throw new Exception(TemplateFile + @"页面未设置<%@ Control Language=""C#"" AutoEventWireup=""false"" Inherits=""CY_MVC.ViewTemplate.Default.TemplateBody"" %>");
            }
        }

        public override string BuildString(IDictionary<string, object> p_ViewData)
        {
            string res = string.Empty;
            if (Template == null)
            {
                Template = new UserControl().LoadControl(TemplateFile.Replace("\\", "/")) as TemplateBody;
            }
            if (Template != null)
            {
                Template.ViewData.Clear();
                foreach (var item in p_ViewData)
                {
                    Template.ViewData.Add(item.Key, item.Value);
                }
                using (TextWriter tw = new StringWriter())
                {
                    using (var htw = new HtmlTextWriter(tw))
                    {
                        Template.RenderControl(htw);
                        res = tw.ToString();
                    }
                }
            }
            return res;
        }
    }

    public class TemplateBody : UserControl
    {
        private IDictionary<string, object> m_ViewData = new Dictionary<string, object>();

        public IDictionary<string, object> ViewData
        {
            get { return m_ViewData; }
            set { m_ViewData = value; }
        }

        
    }
}
